﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using SuperDarts.ScreenManagement;

namespace SuperDarts.Screens.GameScreens
{
    public enum AwardCue
    {
        LowTon,
        HatTrick,
        ThreeInTheBlack,
        HighTon,
        ThreeInABed,
        TonEighty,
        WhiteHorse
    }

    public class AwardScreen : GameScreen
    {
        private bool _loaded;
        private readonly Dictionary<AwardCue, Video> _awards = new Dictionary<AwardCue, Video>();
        private readonly VideoPlayer _videoPlayer;

        public AwardScreen()
        {
            _videoPlayer = new VideoPlayer();
        }

        public void LoadContent(ContentManager content)
        {
            if (!_loaded)
            {
                _awards.Add(AwardCue.HatTrick, content.Load<Video>(@"Awards\HatTrick"));
                _awards.Add(AwardCue.HighTon, content.Load<Video>(@"Awards\HighTon"));
                _awards.Add(AwardCue.LowTon, content.Load<Video>(@"Awards\LowTon"));
                _awards.Add(AwardCue.ThreeInABed, content.Load<Video>(@"Awards\ThreeInABed"));
                _awards.Add(AwardCue.ThreeInTheBlack, content.Load<Video>(@"Awards\ThreeInTheBlack"));
                _awards.Add(AwardCue.TonEighty, content.Load<Video>(@"Awards\TonEighty"));
                _awards.Add(AwardCue.WhiteHorse, content.Load<Video>(@"Awards\WhiteHorse"));
                _loaded = true;
            }
        }

        public void Play(AwardCue cue)
        {
            if (!_awards.Keys.Contains(cue))
            {
                throw new Exception("Award Error");
            }

            _videoPlayer.Volume = SuperDarts.Options.Volume;

            _videoPlayer.Play(_awards[cue]);
            SuperDarts.ScreenManager.AddScreen(this);
        }

        public void Stop()
        {
            _videoPlayer.Stop();

            SuperDarts.ScreenManager.RemoveScreen(this);
        }

        public override void HandleInput(InputState inputState)
        {
            base.HandleInput(inputState);

            if (inputState.MenuEnter || inputState.MenuCancel)
                Stop();
        }

        public override void Update(GameTime gameTime, bool isCoveredByOtherScreen)
        {
            base.Update(gameTime, isCoveredByOtherScreen);

            if (_videoPlayer.PlayPosition.Equals(_videoPlayer.Video.Duration))
            {
                Stop();
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);

            spriteBatch.Begin();
            if (_videoPlayer.State == MediaState.Playing)
                spriteBatch.Draw(_videoPlayer.GetTexture(),
                    new Rectangle(0, 0, SuperDarts.Viewport.Width, SuperDarts.Viewport.Height), Color.White);
            spriteBatch.End();
        }
    }
}